<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        canvas{
            border: 1px solid red;
        }
    </style>
</head>
<body>
<canvas id="box" width="1600" height="1600">您的浏览器不支持画布</canvas>
<script type="text/javascript">
    var box=document.getElementById("box");
    var pen=box.getContext("2d");

    pen.beginPath();
    pen.save();
    pen.clearRect(0,0,box.width,box.height);
    var ele=pen.createLinearGradient(20,20,400,400);
    ele.addColorStop(0,"red");
    ele.addColorStop(0.5,"green");
    ele.addColorStop(1,"blue");

    pen.moveTo(20,20);
    pen.lineTo(400,400);
    pen.strokeStyle=ele;
    pen.lineWidth="30";
    pen.stroke();
    pen.closePath();
    pen.restore();

    var ra=pen.createRadialGradient(250,70,20,200,60,100);
    ra.addColorStop(0,"red");
    ra.addColorStop(0.5,"white");
    ra.addColorStop(1,"black");

    pen.beginPath();
    pen.rect(0,0,400,200);
    pen.strokeStyle=ra;
    pen.fillStyle=ra;
    pen.fill();
    pen.closePath();
    pen.stroke();

</script>
</body>